import Pages from '../../../pages/pages.vue'
import CustomConfig from '../custom-configure/custom-configure.vue'
import { setMenuType, customList, setActivityFlag, showMsg } from '../../../../vuex/action'

export default {
  name: 'custom-list',
  components: {
    'custom-config': CustomConfig,
    'vue-pages': Pages
  },
  data() {
    return {
      showCustomAc: false,
      cusTitle: '',
      custom: {
        id: '',
        name: '',
        startTime: '',
        endTime: ''
      },
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      cList: []
    }
  },
  vuex: {
    getters: {
      fresh: state => state.acCustomListNeedFresh
    },
    actions: {
      xSetMenuType: setMenuType,
      xCustomList: customList,
      xSetActivityFlag: setActivityFlag,
      xShowMsg: showMsg
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xCustomList({
        start: start,
        limit: limit
      })
        .then((data) => {
          this.cList = data.list
          this.pages.totalPages = Math.ceil(data.totalcount / limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    addCustom() {
      this.showCustomAc = true
      this.cusTitle = '新增活动'
      this.custom = {
        name: '',
        startTime: '',
        endTime: ''
      }
    },
    updateCustom(list) {
      this.showCustomAc = true
      this.custom = list
      this.cusTitle = '修改活动'
    },
    saveData(id, name) {
      let data = {
        activityId: +id,
        activityName: name
      }
      this.xSetActivityFlag(data)
    },
    setPrize(id, name) {
      this.saveData(id, name)
      this.$route.router.go('/prize/' + id)
    },
    setFakeRecord(id, name) {
      this.saveData(id, name)
      this.$route.router.go('/fake/record/' + id)
    },
    getPrizeResult(id, name) {
      this.saveData(id, name)
      this.$route.router.go('/result/' + id)
    },
    msgList(id, name) {
      this.saveData(id, name)
      this.$route.router.go('/msg/' + id)
    },
    prizeStatistics(id, name) {
      this.saveData(id, name)
      this.$route.router.go('/result/statistics')
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
